﻿Public Class ViewUser

    Private Sub ViewUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loaddata()

        With DataGridView1
            .EnableHeadersVisualStyles = False
            '.Font = New Font(DataGridView1.Font, FontStyle.Bold)
        End With

        'Dim columnHeaderStyle As New DataGridViewCellStyle
        'columnHeaderStyle.BackColor = Color.LightSkyBlue
        'columnHeaderStyle.ForeColor = Color.Black
        'columnHeaderStyle.Font = _
        '    New Font("Khmer OS Bokor", 12, FontStyle.Bold)
        'DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle
        UI.FormatDataGrid(DataGridView1)
    End Sub

    Private Sub loaddata()
        cmdp.Parameters("@search") = txtsearch.Text & "%"
        DataGridView1.DataSource = cmdp.ExecuteDataTable("SELECT u.*,ur.UserRoleName FROM [User] u INNER JOIN UserRole ur ON u.UserRoleId=ur.UserRoleId WHERE Username like @search")
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Dim frm As New EditUser
        'frm.ShowDialog()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim frm As New EditUser
        frm.l.Text = row.Cells(0).Value.ToString
        frm.txtname.Text = row.Cells(1).Value.ToString
        frm.txtpwd.Text = row.Cells(2).Value.ToString
        frm.rid = row.Cells(3).Value.ToString
        'frm.ShowDialog()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        loaddata()
    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click

        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim id = row.Cells(0).Value

        If IsDBNull(row.Cells(1).Value) Then
            MessageBox.Show("សូមជ្រើសរើសទិន្នន័យត្រូវលុបឱ្យបានត្រឹមត្រូវ")
            Exit Sub
        End If

        If MessageBox.Show("តើអ្នកពិតជាចង់លុបទិន្នន័យនេះមែនទេ?", "លុប", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If
        
        MsgBox(id)
        ' Exit Sub

        cmdp.Parameters("@id") = id
        cmdp.ExecuteNonQuery("DELETE FROM [User] WHERE UserId=@id AND UserId<>1")
        MessageBox.Show("លុបបានសម្រេច")
        loaddata()
    End Sub
End Class